/*
 * This file is part of the quickTools support library, dubbed "LucasCode"
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument d = new XmlDocument();
            d.Load("test.xml");
            XmlNode root = d.SelectSingleNode("UpdateInformation");
            XmlNode prog = d.SelectSingleNode("UpdateInformation/Program");
            richTextBox1.Clear();
            foreach (XmlNode n in root.ChildNodes)
            {
                richTextBox1.Text += "Node: " + n.Name + Environment.NewLine;
            }
            foreach (XmlAttribute a in prog.Attributes)
            {
                richTextBox1.Text += "Attr: " + a.Name + " = " + a.Value + Environment.NewLine;
            }
        }
        XmlAttribute CreateAttr(XmlDocument d, string name, string value)
        {
            XmlAttribute a = d.CreateAttribute(name);
            a.Value = value;
            return a;
        }
        XmlAttribute AddAttr(XmlElement el, string name, string value)
        {
            return el.Attributes.Append(CreateAttr(el.OwnerDocument, name, value));
        }
        XmlElement AddElem(XmlElement el, string name)
        {
            XmlElement elem;
            el.AppendChild(elem = el.OwnerDocument.CreateElement(name));
            return elem;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument d = new XmlDocument();
            XmlNode n;
            XmlElement el,root,mirrors;
            XmlAttribute a;
            /* init */
            d.AppendChild(d.CreateXmlDeclaration("1.0", String.Empty, String.Empty));
            d.AppendChild(d.CreateComment("AutoUpdater Information File"));

            /* create root node */
            d.AppendChild(root = d.CreateElement("UpdateInformation"));


            mirrors = AddElem(root, "Mirrors");

            /* create first element */
            mirrors.AppendChild(el = d.CreateElement("Mirror"));
            AddAttr(el, "Name","Main Mirror");
            AddAttr(el, "Url", "http://lucas.enterprising-developers.de/");

            /* create second element */
            el = AddElem(mirrors, "Mirror");
            AddAttr(el, "Name","Second Mirror");
            AddAttr(el, "Url", "http://lucas.ulm086.server4you.de/");

            //d.AppendChild(n = d.CreateNode(XmlNodeType.Element, "programs", ""));
            el = AddElem(root, "Program");
            AddAttr(el, "Name", "quickLoad");
            AddAttr(el, "Executable", "quickLoad.exe");
            AddAttr(el, "Path", @"C:\Programme\quickLoad");
            AddAttr(el, "RecentVersion", "0.8.0.4");
            AddAttr(el, "UpdateFile", "quickLoad-0.8.0.4.zip");
  
            //n.AppendChild(n2 = d.CreateAttribute("URL"));
            //n2.Value = "http://";
            /*XmlNode n2 = d.CreateNode(XmlNodeType.Attribute, "TEST", "");
            n2.Value = "asdjfaslkdjfalsdf";
            n.AppendChild(n2);*/
            //d.CreateTextNode("Settings");
            d.Save("test.xml");
           
        }
    }
}